﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace SEBanking
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == String.Empty)
                Response.Redirect("~/Index.aspx");

            DataClassesSEBankingDataContext context = new DataClassesSEBankingDataContext();
            var query = context.SEBanking_Cards.Single(c => c.Acc_ID == Session["UserID"].ToString());
            String idCard = query.Card_ID;

            ServiceReferenceSEBanking.Service1Client sw = new ServiceReferenceSEBanking.Service1Client();
            lblBalance.Text = (sw.GetBalance(idCard)).ToString();

            gvExchangeList.DataSource = ListExchange(Session["UserID"].ToString());
            gvExchangeList.DataBind();
        }

        public DataTable ListExchange(string UserName)
        {
            DataClassesSEBankingDataContext context = new DataClassesSEBankingDataContext();

            var query = from c in context.SEBanking_Cards
                        from e in context.SEBanking_Exchanges
                        where c.Acc_ID == UserName && (c.Card_ID == e.Card_ID_Receive || c.Card_ID == e.Card_ID_Send)
                        select new { CardID = c.Card_ID, ExChangeID = e.Exc_ID, Send = e.Card_ID_Send, Receive = e.Card_ID_Receive, Balance = e.Exc_Balance };
            DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("Card ID");
            dt.Columns.Add("Exchange ID");
            dt.Columns.Add("Card ID Send");
            dt.Columns.Add("Card ID Receive");
            dt.Columns.Add("Balance");

            foreach (var q in query)
            {
                System.Data.DataRow dr = dt.NewRow();
                dr[0] = q.CardID;
                dr[1] = q.ExChangeID;
                dr[2] = q.Send;
                dr[3] = q.Receive;
                dr[4] = q.Balance;

                dt.Rows.Add(dr);
            }
            return dt;
        }
    }
}
